package client.gui;

import sharedspace.Question;
import sharedspace.QuestionInTest;
import sharedspace.Subject;
import sharedspace.Teacher;
import sharedspace.User;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.*;

import client.gui.GUI_AbstractMainMenu.MainMenuType;

import messages.*;

/**
 * @This Class give the teacher the option to edit questions
 * @author Amir S.
 * @author Jacob C.
 * @author Maya G.
 * @author Michael V.
 *
 */
public class GUI_Teacher_Edit_Question2 extends JPanel {

	protected String subjectChoosen = null;
	public JButton btnOk=null;
	private JButton btnClear=null;
	public JButton btnBack = null;
	public TextArea textinstruction;
	public TextArea textFielQuestion;
	public TextArea textFieldAnswer1; 
	public TextArea textFieldAnswer2; 
	public TextArea textFieldAnswer3;
	public TextArea textFieldAnswer4; 
	public JLabel title=null;
	public  String submitQuestion;
	public  String submitAnswer[];
	public Client_GUI ms = null;

	private JLabel lblQuestion;
	private JLabel lblAnswer_1;
	private JLabel lblAnswer_2;
	private JLabel lblAnswer_3;
	private JLabel lblAnswer_4;
	private JLabel lblCorrect;
	public JComboBox<String> comboBoxTeacherSubject;
	public JComboBox<String> comboboxCorrectAnswer;
	private Question question;
	public User user;
	private JLabel lblSelectSubjectOf;
	private ArrayList<sharedspace.Subject> s;
	public MessageAllQuestions msgQuestion;
	
	public GUI_Teacher_Edit_Question2(Client_GUI MS,Question questionEdit) {
		super();
		this.ms=MS;
		this.question = questionEdit;
		initialize();
	}
	/***********************************************JUST DESIGN*******************************************************/
	protected void initialize(){
	submitAnswer= new String[3];

	textFielQuestion=new TextArea(100,100);
	this.textFielQuestion.setBounds(new Rectangle(40, 170, 170, 110));
	this.textFielQuestion.setText(question.getQuestion());
	
	textFieldAnswer1=new TextArea(5,20);
	this.textFieldAnswer1.setBounds(new Rectangle(480, 170, 170, 110));
	this.textFieldAnswer1.setText(question.getAnswers()[0]);
	
	textFieldAnswer2=new TextArea(5,20);
	this.textFieldAnswer2.setBounds(new Rectangle(680, 170, 170, 110));
	this.textFieldAnswer2.setText(question.getAnswers()[1]);
	
	textFieldAnswer3=new TextArea(5,20);
	this.textFieldAnswer3.setBounds(new Rectangle(480, 350, 170, 110));
	this.textFieldAnswer3.setText(question.getAnswers()[2]);
	
	textFieldAnswer4=new TextArea(5,20);
	this.textFieldAnswer4.setBounds(new Rectangle(680, 350, 170, 110));
	this.textFieldAnswer4.setText(question.getAnswers()[3]);

	
	title = new JLabel();
	title.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 28));
	title.setBounds(new Rectangle(295, 56, 402, 59));
	title.setForeground(Color.BLUE);
	title.setText("Edit Quesion");
	this.add(title);
	
	lblQuestion = new JLabel("Question:");
	lblQuestion.setBounds(92, 122, 70, 42);
	add(lblQuestion);
	
	lblAnswer_1 = new JLabel("Answer 1:");
	lblAnswer_1.setBounds(522, 123, 70, 42);
	add(lblAnswer_1);
	
	lblAnswer_2 = new JLabel("Answer 2:");
	lblAnswer_2.setBounds(new Rectangle(736, 123, 70, 42));
	add(lblAnswer_2);
	
	lblAnswer_3 = new JLabel("Answer 3:");
	lblAnswer_3.setBounds(522, 307, 64, 42);
	add(lblAnswer_3);
	
	lblAnswer_4 = new JLabel("Answer 4:");
	lblAnswer_4.setBounds(new Rectangle(736, 307, 70, 42));
	add(lblAnswer_4);
	/***********************************************JUST DESIGN*******************************************************/	
	btnBack = new JButton("BACK");	
	btnBack.setFont(new Font("Arial", Font.PLAIN, 16));
	btnBack.setBounds(new Rectangle(76, 612, 107, 30));
	btnBack.setVisible(true);
	
	
		btnOk = new JButton("UPDATE");
		btnOk.setFont(new Font("Arial", Font.PLAIN, 16));
		btnOk.setBounds(new Rectangle(203, 612, 107, 30));
		btnOk.setVisible(true);	
		
		btnOk.addActionListener(new ActionListener() {
			/**
			 * This action call the function  
			 * from the
			 */
			public void actionPerformed(ActionEvent e) {
				
				setTextFields();
				
				if (AllFieldsAreOk()!=false)							//teacher not selected 
				{ 
					updateQuestion();					
				}
			}});// end btnOK listener
				
		
	GUI_ImagePanel background =new GUI_ImagePanel(new ImageIcon("images/background1.jpg").getImage());
	background.setLocation(10, 24);
	background.setSize(new Dimension(1140, 800));
	
	this.setSize(1000, 700);
	this.setLayout(null);
	this.add(btnBack);
	this.add(btnOk);
	this.add(textFielQuestion);
	this.add(textFieldAnswer1);
	this.add(textFieldAnswer2);
	this.add(textFieldAnswer3);
	this.add(textFieldAnswer4);
	this.add(background);
	
	String[] answerStrings = { "Answer1", "Answer2", "Answer3", "Answer4"};
	comboboxCorrectAnswer=new JComboBox<String>(answerStrings);
	comboboxCorrectAnswer.setBounds(150, 380, 200, 30);
	comboboxCorrectAnswer.setSelectedIndex(question.getCorrectAnswer()-1);
	background.add(comboboxCorrectAnswer);
	
	lblCorrect=new JLabel("Choose correct answer: ");
	lblCorrect.setBounds(150, 350, 200, 25);
	background.add(lblCorrect);
	
	JLabel lblInstruction=new JLabel("add instruction for answer:");
	lblInstruction.setBounds(235, 97, 136, 42);
	background.add(lblInstruction);
	
	textinstruction=new TextArea(100,100);
	textinstruction.setBounds(216, 145, 170, 110);
	textinstruction.setText(question.getQuestionCommet());
	background.add(textinstruction);
	
	
		}//end initialize

	/*
	 * Clears all text Fields		   
	 */
	  public void resetTextFields() { 
		  textFielQuestion.setText("");
		  textFieldAnswer1.setText("");
		  textFieldAnswer2.setText("");
		  textFieldAnswer3.setText("");
		  textFieldAnswer3.setText("");
		  textFieldAnswer4.setText("");
		  textinstruction.setText("");
		  
	  }// end reset all fields
		
		  

		/*
		 *  This function add a new question by teacher 
		 */
		public Boolean updateQuestion() {
			setQuestion();
			MessageQuestionUpdate aNq = new MessageQuestionUpdate(question);
			System.out.println("after msgAddNewQuestion");
			try{
				ms.client.sendToServer(aNq);// get list of teacher subject				
				JOptionPane.showMessageDialog(this,"Message was edited successfully");	
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
			return false;
		}  

	  public  void setTextFields() { //set all text Fields
		  
		 submitQuestion=this.getQuestion();
		 submitAnswer=this.getAnswers();
		     
		   }
	  
		public String getQuestion(){				// returns the question
			   return textFielQuestion.getText();
		}
		
		public String[] getAnswers(){				// returns all the answers
			 
			String[] answerTmp=new String[4];
		
				answerTmp[0]=textFieldAnswer1.getText();
				
				answerTmp[1]=textFieldAnswer2.getText();
				answerTmp[2]=textFieldAnswer3.getText();
				answerTmp[3]=textFieldAnswer4.getText();
				for (int i=0;i<4;i++){
				System.out.println(""+answerTmp[i]);
				}
				return answerTmp;
		}

		public Boolean inputCheck(){
													// checks if empty
			if (textFielQuestion.getText()=="" || 
				textFieldAnswer1.getText()=="" ||
				textFieldAnswer2.getText()=="" ||
				textFieldAnswer3.getText()=="" ||
				textFieldAnswer4.getText()==""  )
				return false;
				
				else return true;
			
			
		}// iniitalize
		
		public void showMessage(String message, String title, int messageType) {
			JOptionPane.showMessageDialog(this, message,title,messageType);		
		}
	
		
		/**
		 * This function display teacher Subject list in combo box 
		 * @param teacherArray - is array list of the teacher subjects
		 */
		public void teacherIDComboBox(ArrayList<Subject> teacherSubject) {
			comboBoxTeacherSubject.removeAllItems();  //clear fields 
			if(teacherSubject.size() > 0){   
				comboBoxTeacherSubject.setEnabled(true);
				comboBoxTeacherSubject.addItem("Choose...");
				for (int i=0; i<teacherSubject.size(); i++){
					comboBoxTeacherSubject.addItem(teacherSubject.get(i).getSubjectName());  //display the teacher Subject in combo box
					//comboBoxTeacherSubject.addItem(teacherSubject);
				}
		    }// end if
		    else{
		    	comboBoxTeacherSubject.addItem("Not Available");
		    	comboBoxTeacherSubject.setEnabled(false);
		    }// end else
		}// end function teacherIDComboBox
		
		
		/**
		 * This function returns the Teacher's Subjects from Server
		 */
		public void getTeacherSubject(){
			MessageTeacherSubject mTs = new MessageTeacherSubject(user);
			try{
				ms.client.sendToServer(mTs);// get list of teacher subject
				
				 s = (ArrayList<Subject>)ms.client.GetMessage();
			
				teacherIDComboBox(s); 
				
				
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
			
		}// end getCourses
		
		public void setQuestion(){
			question.setQuestion(submitQuestion);
			question.setAnswers(submitAnswer);
			question.setCorrectAnswer(comboboxCorrectAnswer.getSelectedIndex()+1);
			question.setInUse(false);
			question.setQuestionCommet(textinstruction.getText());
		}
		
		/*
		 * this function check if all fields are fill
		 */
		private boolean AllFieldsAreOk() {
			
			
			for(int j=0;j<4;j++){
				
				//System.out.println(""+submitAnswer[j]);
				if(submitAnswer[j].isEmpty()==true){
				showMessage("Error, you need to fiil out all the answer", "Input Error", JOptionPane.ERROR_MESSAGE);
				
					return false;
				}
		}
			
			//you need to add check for correct answer
				
			if (submitQuestion.isEmpty()==true)	
			{
				
			showMessage("Error, you need to fiil out the Question", "Input Error", JOptionPane.ERROR_MESSAGE);
			return false;
			}
			
			else System.out.println(""+submitQuestion); return true;
		}
		
		public void correctAComboBox() {
			comboboxCorrectAnswer.removeAllItems();  //clear fields   
				comboboxCorrectAnswer.setEnabled(true);
				comboboxCorrectAnswer.addItem("Choose...");
				for (int i=1; i<=4; i++){
					comboboxCorrectAnswer.addItem("Answer"+i);
				}
			
		}
		
}
		

